﻿#include "c_template.h"

c_Template::c_Template()
{
    LastLoadTime=QDateTime::currentDateTime();
    connect(&DllCheck,SIGNAL(timeout()),this,SLOT(Dll_check()),Qt::QueuedConnection);
    DllCheck.start(5000);
}

void c_Template::Dll_check()
{
    if(LastLoadTime.secsTo(QDateTime::currentDateTime())>10)
    {
        LastLoadDll.clear();
        lib.unload();
        LastLoadTime=QDateTime::currentDateTime();
    }
}

void c_Template::readdata(QString data)
{
    QStringList senddata;                            //开始调用read函数
    QStringList information=data.split("***");
    if(information.size()!=3)
    {
        c_send("",readfail,"");
        qDebug()<<"读取任务失败!";
        return;
    }
    QString dll_Name=information[0];
    QString taskid=information[1];
    QString taskdata=information[2];

    typedef QStringList(*FUN1)(QString);
    QString file_path=QCoreApplication::applicationDirPath()+"\\"+"DLL"+"\\"+dll_Name;
    int frequency=0;
    if(LastLoadDll!=file_path)
    {
        lib.unload();
        LastLoadDll=file_path;
        lib.setFileName(file_path);
        while(true)
        {
            if(lib.load())
            {
                FUN1 read = (FUN1)lib.resolve("read");

                if(read)
                {
                    senddata=read(taskdata);
                    LastLoadTime=QDateTime::currentDateTime();
                    break;
                }
                else
                {
                       qDebug()<<"加载read函数失败";
                }
            }
            else
            {
                qDebug()<<"DLL加载失败!";
            }
            frequency++;
            if(frequency>50)
            {
                c_send(taskid,loadfail,"");
                return;
            }
        }
    }
    else
    {
        FUN1 read = (FUN1)lib.resolve("read");
        if(read)
        {
            senddata=read(taskdata);
        }
        else
        {
            c_send(taskid,loadfail,"");
            qDebug()<<"加载read函数失败!";
            return;
        }
    }


    c_send(taskid,read,senddata);
}

void     c_Template::handledata(QString data)
{


    QString send_data;                              //开始调用handle函数
    // qDebug()<<"handle";
    QStringList receivedata=data.split("**&&##");
   // qDebug()<<receivedata;
    if(receivedata.size()!=4)
    {
        c_send("",readfail,"");

        return;
    }

    QString taskid=receivedata[0];
    QString taskbelongid=receivedata[2];
    QString dll_Name=receivedata[1];
    QString taskdata=receivedata[3];
    typedef QString(*FUN1)(QString);
    QString file_path=QCoreApplication::applicationDirPath()+"\\"+"DLL"+"\\"+dll_Name;
    int frequency=0;
    if(LastLoadDll!=file_path)
    {
        lib.unload();
        LastLoadDll=file_path;
        lib.setFileName(file_path);
        while(true)
        {
            if(lib.load())
            {
                FUN1 handle = (FUN1)lib.resolve("handle");
                LastLoadTime=QDateTime::currentDateTime();
                if(handle)
                {
                    send_data=handle(taskdata);
                    break;
                }
                else
                {
                    qDebug()<<"handle函数加载失败！";
                }

            }
            else
            {
                qDebug()<<"DLL加载失败!";
            }
            frequency++;
            if(frequency>100)
            {

                QStringList senddata;
                senddata.append(taskbelongid);
                senddata.append(send_data);
                QString   senddata1=senddata.join("@@@");
                c_send(taskid,loadfail,senddata1);
                return;
            }
        }
    }
    else
    {
        FUN1 handle = (FUN1)lib.resolve("handle");
        if(handle)
        {
            send_data=handle(taskdata);
        }
        else
        {
            QStringList senddata;
            senddata.append(taskbelongid);
            senddata.append(send_data);
            QString   senddata1=senddata.join("@@@");
            c_send(taskid,loadfail,senddata1);
            qDebug()<<"handle函数加载失败!";
            return;
        }
    }

    QStringList senddata;
    senddata.append(taskbelongid);
    senddata.append(send_data);
    QString   senddata1=senddata.join("@@@");
    c_send(taskid,handle,senddata1);

}

void     c_Template::sumdata(QString Taskid,QString Tasktype,QStringList data)
{
    QString senddata;                               //开始调用sum函数
    QString taskid;
    //   qDebug()<<"sum";


    QString dll_Name=Tasktype;
    typedef QString(*FUN1)(QStringList);
    QString file_path=QCoreApplication::applicationDirPath()+"\\"+"DLL"+"\\"+dll_Name;
    int frequency=0;
    if(LastLoadDll!=file_path)
    {
        lib.unload();
        LastLoadDll=file_path;
        lib.setFileName(file_path);
        while(true)
        {
            if(lib.load())
            {
                FUN1 sum = (FUN1)lib.resolve("sum");
                LastLoadTime=QDateTime::currentDateTime();
                if(sum)
                {
                    senddata=sum(data);
                    break;
                }

            }
            else
            {
                qDebug()<<"DLL加载失败!";
            }
            frequency++;
            if(frequency>100)
            {
                c_send(taskid,loadfail,senddata);
                return;
            }
        }
    }
    else
    {
        FUN1 sum = (FUN1)lib.resolve("sum");
        if(sum)
        {
            senddata=sum(data);
        }
        else
        {
            c_send(taskid,loadfail,senddata);
            qDebug()<<"加载sum函数失败";
            return;
        }
    }
    //   qDebug()<<data<<senddata;
    c_send(Taskid,sum,senddata);
}

void     c_Template::receive_data(task_type type, QString data, QObject *object)
{
    if(object!=this)
    {
        return;
    }
    switch (type) {
    case read:
        readdata(data);
        break;
    case handle:
        handledata(data);
        break;
    case sum:
    {
        QStringList dealdata=data.split("***");
        if(dealdata.size()!=3)
        {
            c_send("",readfail,"");
            return;
        }
        QString Taskid=dealdata.at(0);
        QString Tasktype=dealdata.at(1);
        QStringList Taskdata=dealdata.at(2).split("!!!");
        //  qDebug()<<Taskid<<Tasktype<<Taskdata;
        sumdata(Taskid,Tasktype,Taskdata);
        break;
    }
    default:
    {
        QStringList dealdata=data.split("***");
        if(dealdata.size()!=3)
        {
            c_send("",readfail,"");
            return;
        }
        QString Taskid=dealdata.at(0);
        qDebug()<<"计算模块解析遇到未知类型";
        c_send(Taskid,readfail,"");
        break;
    }
    }
}

void        c_Template::c_send(QString taskid, task_type type, QString data)
{
    switch (type) {
    case handle:
    {
        QString senddata=taskid+"@@@"+data;
        emit send_data(type,senddata);
        break;
    }
    case sum:
    {
        QString senddata=taskid+"@@@"+data;
        //    qDebug()<<senddata;
        emit send_data(type,senddata);
        break;
    }
    case loadfail:
    {
        QString senddata=taskid+"@@@"+data;
        emit send_data(type,senddata);
        break;
    }
    case readfail:
    {
        QString senddata=taskid+"@@@"+data;
        emit send_data(type,senddata);
        break;
    }
    default:
        QString senddata=taskid;
        qDebug()<<"未知类型!";
        emit send_data(type,senddata);
        break;
    }
}

void       c_Template::c_send(QString taskid,task_type type, QStringList data)
{
    switch (type) {
    case read:
    {
        QString dealdata=taskid+"@@@"+data.join("&&**");
        emit    send_data(type,dealdata);

        break;
    }
    default:
        QString senddata=taskid;
        qDebug()<<"未知类型";
        emit    send_data(type,senddata);

        break;
    }
}
